package com.j1903.util;

import com.qiniu.common.Zone;

import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.InputStream;
import java.util.UUID;

@Slf4j
public class QiNiuUtil {
    //访问key
    private String AccessKey="CaRYRJw_JspEdD7dHRUn4xikQ_OxHXtWy4CDI1pl";
    //访问密钥
    private String SecretKey="bqbbpcd3TsA1e-GEvYYmHFbGxiAijEHjMJrP8gp4";
    //空间名
    private String bucketName="cy3452519246";
    //密钥配置对象
    private  Auth auth = Auth.create(AccessKey,SecretKey);
    //配置服务器区域对象
    private Zone z = Zone.autoZone();
    private Configuration config = new Configuration(z);
    //创建上传对象
    private UploadManager um = new UploadManager(config);
    //根据空间名获取访问token
    private String getToken(){
        return auth.uploadToken(this.bucketName);
    }
    public String upload(InputStream is){
        //上传到七牛云的文件名
        String fileName= UUID.randomUUID().toString()
                .replace("-","")+".jpg";
        try{
            //上传图片到七牛云
            Response res = um.put(is,fileName,getToken(),null,null);
            return res.bodyString();
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
        }
        return null;
    }
}
